/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article_spec                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： Article_specService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.Common;
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class Article_specService: IArticle_specService
    {
        private readonly IArticle_specRepository _repository;

        public Article_specService(IArticle_specRepository repository)
        {
            _repository = repository;
        }

        public bool Delete(int id)
        {
            return _repository.DeleteAll(id);
        }

        public List<Article_spec> GetList(string conditions)
        {
            var list= _repository.GetList(conditions).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                var values = _repository.GetList("where parent_id=" + list[i].id);
                List<Article_spec_value> listvalue = new List<Article_spec_value>();
                foreach (var item in values)
                {
                    Article_spec_value model = new Article_spec_value();
                    model.id = item.id;
                    model.parent_id = item.parent_id;
                    model.img_url = item.img_url;
                    model.sort_id = item.sort_id;
                    model.title = item.title;
                    listvalue.Add(model);
                }
                list[i].values = listvalue;
            }
            return list;
        }

        public bool UpdateField(int id, string strValue)
        {
            return _repository.UpdateField(id, strValue);
        }
        public List<Models.Article_spec> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }
        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }

        public bool Exists(int id)
        {
            return GetModel(id) != null ? true : false;
        }

        public Article_spec GetModel(int id)
        {
            var model = _repository.Get(id);
            if (model == null)
                return null;
            var list = _repository.GetList("where parent_id=@parent_id", new { parent_id = id }).ToList();
            List<Models.Article_spec_value> models = new List<Models.Article_spec_value>();
            Models.Article_spec_value modelt;
            for (int n = 0; n < list.Count; n++)
            {
                modelt = new Models.Article_spec_value();
                modelt.id = list[n].id;
                modelt.parent_id = list[n].parent_id;
                modelt.title = list[n].title;
                modelt.sort_id = list[n].sort_id;
                modelt.img_url = list[n].img_url;
                models.Add(modelt);
            }
            model.values = models;
            return model;
        }

        public int Add(Article_spec model)
        {
            int id = (int)_repository.Insert(model);
            if(model.values!=null)
            {
                foreach (var item in model.values)
                {
                    _repository.Insert(new Article_spec { parent_id = id, title = item.title, sort_id = item.sort_id, img_url = item.img_url });
                }                
            }
            return id;
        }

        public bool Update(Article_spec model)
        {
            //删除已删除的规格值
            DeleteValues(model.values, model.id);
            _repository.Update(model);
            if(model.values!=null)
            {
                foreach (var item in model.values)
                {
                    var mm = GetModel(item.id);
                    if (mm != null)
                    {
                        mm.img_url = item.img_url;
                        mm.title = item.title;
                        mm.sort_id = item.sort_id;
                        mm.parent_id = item.parent_id;
                        _repository.Update(mm);
                    }
                    else
                    {
                        _repository.Insert(new Article_spec { parent_id = model.id, title = item.title, sort_id = item.sort_id, img_url = item.img_url });
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 查找不存在的规格值并删除
        /// </summary>
        private void DeleteValues(List<Models.Article_spec_value> models, int parentId)
        {
            StringBuilder idList = new StringBuilder();
            if (models != null)
            {
                foreach (Models.Article_spec_value modelt in models)
                {
                    if (modelt.id > 0)
                    {
                        idList.Append(modelt.id + ",");
                    }
                }
                string id_list = Utils.DelLastChar(idList.ToString(), ",");
                if (!string.IsNullOrEmpty(id_list))
                {
                    _repository.Delete(parentId, id_list);
                }
            }
        }

        public List<Article_spec> GetCategorySpecList(int category_id)
        {
            return _repository.GetCategorySpecList(category_id);
        }
    }
}